package com.example.sj12;
import android.content.ContentValues;
import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;


public class RegisterActivity extends AppCompatActivity {
    private EditText username;
    private EditText password,password2;
    private Button login;
    private Button register;
    private RadioButton btc;
    private RadioButton bts;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);//即activity_login.xml
        findViews();
        register.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
        String testname =username.getText().toString().trim();//.trim()去掉字符串两端的bai多du余的空格,注册使用，登录就不必再验证
        String testpass=password.getText().toString().trim();
        String testpass2 = password2.getText().toString().trim();
                //判断输入框内容
                if(TextUtils.isEmpty(testname)){
                    Toast.makeText(RegisterActivity.this, "请输入用户名", Toast.LENGTH_SHORT).show();
                    return;
                }else if(TextUtils.isEmpty(testpass)){
                    Toast.makeText(RegisterActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
                    return;
                }else if(TextUtils.isEmpty(testpass2)){
                    Toast.makeText(RegisterActivity.this, "请再次输入密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                else if(!testpass.equals(testpass2)){
                    Toast.makeText(RegisterActivity.this, "输入两次的密码不一样", Toast.LENGTH_SHORT).show();
                    return;}
                new Thread(new Runnable() {
                    private static final String TAG = "测试";

                    @Override
                    public void run() {
                        String n = username.getText().toString().trim();
                        String psw = password.getText().toString().trim();
                        UserDao ud = new UserDao();
                        //boolean result =ud.register(n,psw);
                        if (ud.check(n,btc.isChecked())) {
                            Looper.prepare();
                            Toast.makeText(RegisterActivity.this, "该用户名已被注册，注册失败", Toast.LENGTH_SHORT).show();
                            Looper.loop();
                        } else {

                            //UserDao u = new UserDao();
                            //boolean t=u.register(n, psw);
                            if (!ud.register(n, psw,btc.isChecked())) {
                                Looper.prepare();
                                Toast.makeText(RegisterActivity.this, "插入数据表成功", Toast.LENGTH_SHORT).show();
                                Looper.loop();
                            }
                        /*if (!result){
                            Looper.prepare();
                            Toast toast = Toast.makeText(RegisterActivity.this,"注册成功！",Toast.LENGTH_SHORT);
                            toast.show();
                            Looper.loop();
                        }*/
                            Log.i(TAG, "fun" + ud.register(n, psw,btc.isChecked()));
                            //以上为jdbc注册
                        }
                    }
                }).start();
            }
    });
    }



    private void findViews() {
        username=(EditText) findViewById(R.id.login_accout);
        password=(EditText) findViewById(R.id.login_password);
        password2= (EditText) findViewById(R.id.login_password2);
        register=(Button) findViewById(R.id.login_register);
        login=(Button) findViewById(R.id.login_login);
        btc=findViewById(R.id.customer);
        bts=findViewById(R.id.store);
        login.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent intent=new Intent(RegisterActivity.this, LoginActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }

}

